package java101;
import javax.swing.JOptionPane;

public class NumberNamer {
	public static void main(String[] args) {

		String input = JOptionPane.showInputDialog
		("Enter a number < 1,000,000,000,000: ");
		double i = Double.parseDouble(input);
		
		for (int c = 4; c >= 0; --c) {
			double divisor = (double) Math.pow(10, c*3);
			double level = (double) Math.floor(i/(divisor));
			if (level > 0) {
				Script(level, c);
			}
			i = (double) (i - (level * divisor));	
		}
		System.out.println(" ==> " + input);
	}

	private static void Script(double level, int c) {
		String[] ones = {" ", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "forteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
		String[] tens = {" ", " ", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
		String[] hundreds = {"thousand", "million", "billion"};
		if (level > 99) {
			int hundred = (int) Math.floor(level/100);
			System.out.print(ones[hundred] + " hundred ");
			level = level - (hundred * 100);
		}
		if (level<20) {
			System.out.print(ones[(int) level] + " ");
			if (c == 0) {
				System.exit(0);
			}
			System.out.print(hundreds[c-1] + " ");
			return;
		}
		if (level>0) {
			int ten = (int) Math.floor(level/10);
			System.out.print(tens[ten] + " ");
			level = level - (ten * 10);
			System.out.print(ones[(int) level] + " ");
		}
		if (c>0) {
			System.out.print(hundreds[c-1] + " ");
			return;
		}
	}
}
